package com.itrip.controller;

import com.itrip.document.Hotel;
import com.itrip.dto.ResponseDTO;
import com.itrip.pojo.ItripHotelOrder;
import com.itrip.pojo.ItripUser;
import com.itrip.service.OrderService;
import com.itrip.vo.HotelSelectVo;
import com.itrip.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "订单列表")
@RestController
public class OrderController {
    @Resource
    private OrderService orderService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @ApiOperation("订单添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "登录令牌",paramType = "header",dataType = "String")
    })
    @PostMapping("view/addOrder")
    public ResponseDTO addOrder(OrderVo orderVo, HttpServletRequest request) {
        //创建一个订单对象
        ItripHotelOrder itripHotelOrder = new ItripHotelOrder();
        //通过redis获取用户
        String token = request.getHeader("token");
        ItripUser itripUser = (ItripUser)redisTemplate.opsForValue().get(token);
        itripHotelOrder.setUserId(itripUser.getId());

        itripHotelOrder.setOrderType(1);

        //通过时间戳生成订单
        long l = System.currentTimeMillis();
        String s = String.valueOf(l);
        String prefix=s.substring(s.length()-12);

        String s1 = orderVo.getRoomId().toString();
        StringBuilder suffix=new StringBuilder(s1);
        for(int i=suffix.length();i<=6;i++){
            suffix.append(0);
        }
        String orderNo=prefix+suffix;
        itripHotelOrder.setOrderNo(orderNo);
        itripHotelOrder.setTradeNo(orderNo);

        itripHotelOrder.setHotelId((long)orderVo.getHotelId());

        //查询酒店名称放入订单对象

        //
        itripHotelOrder.setRoomId((long)orderVo.getRoomId());

        itripHotelOrder.setCount(orderVo.getCount());

        itripHotelOrder.setCheckInDate(orderVo.getCheckInDate());

        itripHotelOrder.setCheckOutDate(orderVo.getCheckOutDate());

        itripHotelOrder.setOrderStatus(0);

        itripHotelOrder.setPayAmount(orderVo.getPayAmount());

        itripHotelOrder.setNoticePhone(orderVo.getNoticePhone());

        itripHotelOrder.setLinkUserName(orderVo.getLinkUserName());

        itripHotelOrder.setCreationDate(new Date());

        //
        long days=(orderVo.getCheckOutDate().getTime()-orderVo.getCheckInDate().getTime())/(1000*60*60*24);
        itripHotelOrder.setBookingDays((int)days);

        /*添加一个Order的失效时间*/
        //键值格式   OrderNO:roomId,Count(消耗房间数量)
        redisTemplate.opsForValue().set("order:"+itripHotelOrder.getOrderNo()+","+itripHotelOrder.getHotelId()+","+itripHotelOrder.getRoomId()+","+itripHotelOrder.getCount(),itripHotelOrder, 30,TimeUnit.MINUTES);


        int state =orderService.addOrder(itripHotelOrder);
        return new ResponseDTO(100,"添加成功",null);
    }

    @ApiOperation("查询用户是否有未支付订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "登录令牌",paramType = "header",dataType = "String"),
            @ApiImplicitParam(name="userId",value = "用户id",example = "12")
    })
    @PostMapping("view/checkUnpayOrder")
    public ResponseDTO checkUnpayOrder(int userId){
        //获取未支付订单数
        int count = orderService.checkUnpayOrder((userId));
        return new ResponseDTO(100,"查询未支付订单成功",count);
    }


    @ApiOperation("查询用户订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "登录令牌",paramType = "header",dataType = "String"),
    })
    @PostMapping("view/getOrderInfo")
    public ResponseDTO getOrderInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        ItripUser itripUser = (ItripUser)redisTemplate.opsForValue().get(token);
        Long id = itripUser.getId();
        ItripHotelOrder itripHotelOrder=orderService.getOrderInfo(id);
        if(itripHotelOrder==null)
            return new ResponseDTO(101,"订单信息部存在",null);
        return new ResponseDTO(100,"订单信息查询成功",itripHotelOrder);
    }


}
